package com.home.action;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.home.common.Cache;
import com.home.common.FileOperation;
import com.home.common.Page;
import com.home.model.Area;
import com.home.model.Home;
import com.home.model.Price;
import com.home.model.QueryHome;
import com.home.model.Rent;
import com.home.service.HomeService;

@Controller
public class HomeAction {

	@Autowired
	private HomeService homeService;

	@RequestMapping(value = "/index.do")
	public String getAllHome(HttpServletRequest request, Integer currentPage,
			QueryHome home) {
		if (currentPage == null || currentPage == 0) {
			currentPage = 1;
		}
		Page page = new Page();
		page.setPageNo(currentPage);
		page.setPageSize(5);
		home.setType("rent");
		this.setQuery(home);
		this.homeService.getHome(page, home);
		setCacheToRequest(request);
		request.setAttribute("home", home);
		request.setAttribute("page", page);

		return "index";
	}
	
	
	@RequestMapping(value = "/sell/list.do")
	public String getAllSellHome(HttpServletRequest request, Integer currentPage,
			QueryHome home) {
		if (currentPage == null || currentPage == 0) {
			currentPage = 1;
		}
		Page page = new Page();
		page.setPageNo(currentPage);
		page.setPageSize(5);
		home.setType("sell");
		this.setQuery(home);
		this.homeService.getHome(page, home);
		setCacheToRequest(request);
		request.setAttribute("home", home);
		request.setAttribute("page", page);

		return "selllist";
	}
	
	@RequestMapping(value = "/renthome/get.do")
	public String rentGet(HttpServletRequest request, Integer id) {
		if (id != null) {
			Home home = getHomeService().getHomeById(id);
			request.setAttribute("home", home);
		}
		setCacheToRequest(request);
		return "renthome";
	}
	@RequestMapping(value = "/sellhome/get.do")
	public String sellGet(HttpServletRequest request, Integer id) {
		if (id != null) {
			Home home = getHomeService().getHomeById(id);
			request.setAttribute("home", home);
		}
		setCacheToRequest(request);
		return "sellhome";
	}


	@RequestMapping(value = "/home/list.do")
	public String getHomeByFilster(HttpServletRequest request, Home home) {
		Page<Home> p = new Page<Home>();
		p.orderBy("publishDate");
		p.order("desc");
		return "test";
	}

	@RequestMapping(value = "/admin/renthome/list.do")
	public String adminRentList(HttpServletRequest request,
			Integer currentPage, QueryHome home) {
		if (currentPage == null || currentPage == 0) {
			currentPage = 1;
		}
		Page page = new Page();
		page.setPageNo(currentPage);
		page.setPageSize(5);
		home.setType("rent");
		this.setQuery(home);
		this.homeService.getHome(page, home);
		setCacheToRequest(request);
		request.setAttribute("home", home);
		request.setAttribute("page", page);
		return "/admin/renthome/list";
	}

	@RequestMapping(value = "/admin/sellhome/list.do")
	public String adminSellList(HttpServletRequest request,
			Integer currentPage, QueryHome home) {
		if (currentPage == null || currentPage == 0) {
			currentPage = 1;
		}
		Page page = new Page();
		page.setPageNo(currentPage);
		page.setPageSize(5);
		home.setType("sell");
		this.setQuery(home);
		this.homeService.getHome(page, home);
		setCacheToRequest(request);
		request.setAttribute("home", home);
		request.setAttribute("page", page);
		return "/admin/sellhome/list";
	}

	@RequestMapping(value = "/admin/renthome/get.do")
	public String adminRentGet(HttpServletRequest request, Integer id) {
		if (id != null) {
			Home home = getHomeService().getHomeById(id);
			request.setAttribute("home", home);
		}
		setCacheToRequest(request);
		return "/admin/renthome/obj";
	}

	@RequestMapping(value = "/admin/sellhome/get.do")
	public String adminSellGet(HttpServletRequest request, Integer id) {
		if (id != null) {
			Home home = getHomeService().getHomeById(id);
			request.setAttribute("home", home);
		}
		setCacheToRequest(request);
		return "/admin/sellhome/obj";
	}

	@RequestMapping(value = "/admin/renthome/save.do")
	public String addRentHome(@RequestParam("imgFile")
	MultipartFile imgFile, Home home) throws IOException {

		if (imgFile != null && imgFile.getOriginalFilename() != null
				&& !imgFile.getOriginalFilename().trim().equals("")) {
			String name = FileOperation.save(imgFile.getOriginalFilename(),
					imgFile.getInputStream());
			home.setBigIcon(name);
			home.setSmallIcon(name);
		}

		home.setPublishDate(new Date());
		this.getHomeService().insert(home);

		return "redirect:/admin/renthome/get.do?id=" + home.getId();
	}

	@RequestMapping(value = "/admin/sellhome/save.do")
	public String addSellHome(@RequestParam("imgFile")
	MultipartFile imgFile, Home home) throws IOException {

		if (imgFile != null && imgFile.getOriginalFilename() != null
				&& !imgFile.getOriginalFilename().trim().equals("")) {
			String name = FileOperation.save(imgFile.getOriginalFilename(),
					imgFile.getInputStream());
			home.setBigIcon(name);
			home.setSmallIcon(name);
		}

		home.setPublishDate(new Date());
		this.getHomeService().insert(home);

		return "redirect:/admin/sellhome/get.do?id=" + home.getId();
	}

	@RequestMapping(value = "/admin/renthome/delete.do")
	public String delRentHome(Integer id) throws IOException {
		this.getHomeService().deleteHomeById(id);
		return "redirect:/admin/renthome/list.do";
	}

	@RequestMapping(value = "/admin/sellhome/delete.do")
	public String delsellHome(Integer id) throws IOException {
		this.getHomeService().deleteHomeById(id);
		return "redirect:/admin/sellhome/list.do";
	}

	private void setHome(QueryHome home) {
		if (home.getRent() != null && !home.getRent().trim().equals("")) {
			home.setRentBegin(Cache.getInstance().getRent(home.getRent())
					.getBegin());
			home.setRentEnd(Cache.getInstance().getRent(home.getRent())
					.getEnd());
		}
		if (home.getPrice() != null && !home.getPrice().trim().equals("")) {
			home.setPriceBegin(Cache.getInstance().getPrice(home.getPrice())
					.getBegin());
			home.setPriceEnd(Cache.getInstance().getPrice(home.getPrice())
					.getEnd());
		}
		if (home.getArea() != null && !home.getArea().trim().equals("")) {
			home.setAreaBegin(Cache.getInstance().getArea(home.getArea())
					.getBegin());
			home.setAreaEnd(Cache.getInstance().getArea(home.getArea())
					.getEnd());
		}

	}

	private void setQuery(QueryHome home) {
		Cache cache = Cache.getInstance();
		if (home.getArea() != null && !home.getArea().equals("")) {
			Area area = cache.getArea(home.getArea());
			if (area != null) {
				home.setAreaBegin(area.getBegin());
				home.setAreaEnd(area.getEnd());
			}
		}

		if (home.getRent() != null && !home.getRent().equals("")) {
			Rent rent = cache.getRent(home.getRent());
			if (rent != null) {
				home.setRentBegin(rent.getBegin());
				home.setRentEnd(rent.getEnd());
			}
		}

		if (home.getPrice() != null && !home.getPrice().equals("")) {
			Price price = cache.getPrice(home.getPrice());
			if (price != null) {
				home.setPriceBegin(price.getBegin());
				home.setPriceEnd(price.getEnd());
			}
		}

	}

	private void setCacheToRequest(HttpServletRequest request) {
		Cache cache = Cache.getInstance();
		request.setAttribute("zoneList", cache.getZone());
		request.setAttribute("priceList", cache.getPrice());
		request.setAttribute("fixList", cache.getFix());
		request.setAttribute("houseTypeList", cache.getType());

		request.setAttribute("areaList", cache.getArea());
		request.setAttribute("houseAgeList", cache.getHouseAge());
		request.setAttribute("rentList", cache.getRent());
		request.setAttribute("houseUseList", cache.getHouseUse());
	}

	@RequestMapping(value = "/test/get.do")
	public String testLogin(HttpServletRequest request) {
		return "login";
	}

	public void setHomeService(HomeService homeService) {
		this.homeService = homeService;
	}

	public HomeService getHomeService() {
		return homeService;
	}

}
